class Task {
  String id;
  String title;
  bool isCompleted;
  DateTime time;
  DateTime? completedTime;
  int priority;
  String location;
  String category;
  bool isDeleted;
  String? note;

  Task({
    required this.id,
    required this.title,
    this.isCompleted = false,
    required this.time,
    this.completedTime,
    this.priority = 1,
    this.location = 'Home',
    this.category = 'Personal',
    this.isDeleted = false,
    this.note,
  });

  static List<Task> getSampleTasks() {
    final now = DateTime.now();
    final today = DateTime(now.year, now.month, now.day);
    
    return [
      Task(
        id: '1',
        title: 'Cleaning Clothes',
        time: DateTime(today.year, today.month, today.day, 7, 0),
        priority: 3,
        location: 'Home',
        category: 'Personal',
        note: '包括床单和被套，需要分类洗涤',
      ),
      Task(
        id: '2',
        title: 'Team Meeting',
        time: DateTime(today.year, today.month, today.day, 7, 15),
        priority: 3,
        location: 'Office',
        category: 'Meeting',
        note: '讨论下周项目进度和资源分配',
      ),
      Task(
        id: '3',
        title: 'Project Review',
        time: DateTime(today.year, today.month, today.day, 8, 0),
        priority: 3,
        location: 'Office',
        category: 'Work',
        note: '审查UI设计稿和技术文档',
      ),
      Task(
        id: '4',
        title: 'Call with Client',
        time: DateTime(today.year, today.month, today.day, 8, 0),
        priority: 2,
        location: 'Office',
        category: 'Work',
        note: '确认需求变更和交付时间',
      ),
      Task(
        id: '5',
        title: 'Prepare Presentation',
        time: DateTime(today.year, today.month, today.day, 10, 0),
        priority: 1,
        location: 'Office',
        category: 'Work',
        note: '制作下周客户演示的PPT',
      ),
      Task(
        id: '6',
        title: 'Pick up Kids',
        time: DateTime(today.year, today.month, today.day, 15, 0),
        priority: 2,
        location: 'School',
        category: 'Personal',
        note: '记得带小明的书包和水杯',
      ),
      Task(
        id: '7',
        title: 'Dentist Appointment',
        time: DateTime(today.year, today.month, today.day + 1, 9, 0),
        priority: 2,
        location: 'Clinic',
        category: 'Personal',
        note: '定期检查，带好医保卡',
      ),
      Task(
        id: '8',
        title: 'Pay Bills',
        time: DateTime(today.year, today.month, today.day + 1, 14, 0),
        priority: 2,
        location: 'Home',
        category: 'Private',
        note: '水电费、网费和信用卡账单',
      ),
      Task(
        id: '9',
        title: 'Birthday Party',
        time: DateTime(today.year, today.month, today.day + 2, 18, 0),
        priority: 1,
        location: 'Restaurant',
        category: 'Events',
        note: '小李的生日派对，已准备礼物',
      ),
      Task(
        id: '10',
        title: 'Team Building',
        time: DateTime(today.year, today.month, today.day + 3, 10, 0),
        priority: 1,
        location: 'Park',
        category: 'Meeting',
        note: '户外拓展活动，记得穿运动鞋',
      ),
      Task(
        id: '11',
        title: 'Doctor Appointment',
        time: DateTime(today.year, today.month, today.day + 3, 15, 30),
        priority: 3,
        location: 'Hospital',
        category: 'Private',
        note: '体检报告复查，早上需要空腹',
      ),
      Task(
        id: '12',
        title: 'Conference',
        time: DateTime(today.year, today.month, today.day + 4, 9, 0),
        priority: 2,
        location: 'Convention Center',
        category: 'Work',
        note: '技术大会，重点关注AI相关主题',
      ),
      Task(
        id: '13',
        title: 'Movie Night',
        time: DateTime(today.year, today.month, today.day + 4, 19, 0),
        priority: 1,
        location: 'Cinema',
        category: 'Personal',
        note: '和朋友一起看新上映的科幻片',
      ),
      Task(
        id: '14',
        title: 'Family Gathering',
        time: DateTime(today.year, today.month, today.day + 5, 12, 0),
        priority: 2,
        location: 'Home',
        category: 'Events',
        note: '亲戚聚会，准备家常菜',
      ),
      Task(
        id: '15',
        title: 'Yoga Class',
        time: DateTime(today.year, today.month, today.day + 5, 7, 0),
        priority: 1,
        location: 'Gym',
        category: 'Personal',
        note: '晨练瑜伽课程，带上瑜伽垫',
      ),
      Task(
        id: '16',
        title: 'Project Deadline',
        time: DateTime(today.year, today.month, today.day + 6, 17, 0),
        priority: 3,
        location: 'Office',
        category: 'Work',
        note: '最终版本提交，需要完成测试',
      ),
      Task(
        id: '17',
        title: 'Lunch with Friend',
        time: DateTime(today.year, today.month, today.day + 6, 12, 30),
        priority: 1,
        location: 'Restaurant',
        category: 'Personal',
        note: '老同学聚会，约在川菜馆',
      ),
      Task(
        id: '18',
        title: 'Tax Filing',
        time: DateTime(today.year, today.month, today.day + 7, 10, 0),
        priority: 3,
        location: 'Home',
        category: 'Private',
        note: '年度个税申报，准备相关资料',
      ),
      Task(
        id: '19',
        title: 'Team Presentation',
        time: DateTime(today.year, today.month, today.day + 7, 15, 0),
        priority: 2,
        location: 'Office',
        category: 'Meeting',
        note: '向管理层汇报季度工作成果',
      ),
      Task(
        id: '20',
        title: 'Concert',
        time: DateTime(today.year, today.month, today.day + 8, 20, 0),
        priority: 1,
        location: 'Stadium',
        category: 'Events',
        note: '周杰伦演唱会，提前2小时到场',
      ),
      Task(
        id: '21',
        title: 'Strategy Meeting',
        time: DateTime(today.year, today.month, today.day + 8, 14, 0),
        priority: 2,
        location: 'Office',
        category: 'Work',
        note: '制定下个季度的业务策略',
      ),
    ];
  }

  static Map<String, int> getTaskCountByCategory(List<Task> tasks) {
    final countMap = <String, int>{
      'Personal': 0,
      'Work': 0,
      'Private': 0,
      'Meeting': 0,
      'Events': 0,
    };
    
    for (var task in tasks) {
      if (countMap.containsKey(task.category)) {
        countMap[task.category] = countMap[task.category]! + 1;
      }
    }
    
    return countMap;
  }

  // JSON序列化方法
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'isCompleted': isCompleted,
      'time': time.millisecondsSinceEpoch,
      'completedTime': completedTime?.millisecondsSinceEpoch,
      'priority': priority,
      'location': location,
      'category': category,
      'isDeleted': isDeleted,
      'note': note,
    };
  }

  // JSON反序列化方法
  factory Task.fromJson(Map<String, dynamic> json) {
    return Task(
      id: json['id'] as String,
      title: json['title'] as String,
      isCompleted: json['isCompleted'] as bool? ?? false,
      time: DateTime.fromMillisecondsSinceEpoch(json['time'] as int),
      completedTime: json['completedTime'] != null 
          ? DateTime.fromMillisecondsSinceEpoch(json['completedTime'] as int)
          : null,
      priority: json['priority'] as int? ?? 1,
      location: json['location'] as String? ?? 'Home',
      category: json['category'] as String? ?? 'Personal',
      isDeleted: json['isDeleted'] as bool? ?? false,
      note: json['note'] as String?,
    );
  }
}